﻿' Copyright (C) 2015 HTTPBuilder
'
' This program is free software: you can redistribute it and/or modify it under the +terms 
' of the GNU General Public License as published by the Free Software Foundation, either 
' version 3 of the License, or (at your option) any later version.

' This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
' without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
' See the GNU General Public License for more details.

' You should have received a copy of the GNU General Public License along with this program.
' If not, see http://www.gnu.org/licenses/. 

Imports System.IO
Imports System.Text

''' <summary>
''' Provides readable text output by indenting and outdenting lines.
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class IndentWriter

#Region "Member variables"
    Private ReadOnly _result As New StringBuilder()
    Private _indentLevel As Integer
    Private _indentString As String = ""
#End Region

#Region "Constructor"
    ''' <summary>
    ''' Returns a new instance of the indent writer.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub New()
        Me.New("    ")
    End Sub
    ''' <summary>
    ''' Returns a new instance of the indent writer, using the <paramref name="indentString">indentString</paramref> parameter.
    ''' </summary>
    ''' <param name="indentString">Indent string.</param>
    ''' <remarks></remarks>
    Public Sub New(indentString As String)
        _indentString = indentString
    End Sub
#End Region

#Region "Public methods"
    ''' <summary>
    ''' Indent level of text.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Indent()
        _indentLevel += 1
    End Sub

    ''' <summary>
    ''' Outdent level of text.
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub Outdent()
        If _indentLevel > 0 Then
            _indentLevel -= 1
        End If
    End Sub

    ''' <summary>
    ''' Write a line to the output.
    ''' </summary>
    ''' <param name="line">Line to output.</param>
    ''' <remarks></remarks>
    Public Sub WriteLine(line As String)
        _result.AppendLine(CreateIndent() & line)
    End Sub

    ''' <summary>
    ''' Returns the indented result.
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function ToString() As String
        Return _result.ToString()
    End Function
#End Region

#Region "Private methods"
    ''' <summary>
    ''' Creates the indent string.
    ''' </summary>
    ''' <returns>String based on level.</returns>
    ''' <remarks></remarks>
    Private Function CreateIndent() As String
        Dim indent As New StringBuilder()

        For i As Integer = 0 To _indentLevel - 1
            indent.Append(_indentString)
        Next
        Return indent.ToString()
    End Function
#End Region
End Class
